﻿using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace SlidingPuzzle {

    /// <summary>
    /// Options window to customize sliding puzzle game.
    /// <BR>
    /// Author: David I. Schwartz (dis@mail.rit.edu)
    /// <BR>
    /// </summary>
    public partial class CustomGame : Form {

        #region Fields and Properties

        private SlidingPuzzle mainWindow; // link back to main window
        private string imageFileName;     // current image filename
        private Color color;              // current selected tile color
        private bool numbersOn;           // enable tile numbers?
        private bool timerOn;             // enable timer?
        private bool movesOn;             // enable moves counter?
        private int tileCount;            // size of board

        /// <summary>
        /// Return board size.
        /// </summary>
        public int TileCount { get { return tileCount; } }

        /// <summary>
        /// Return name of image file.
        /// </summary>
        public string ImageFileName { get { return imageFileName; } }

        /// <summary>
        /// Return selected tile color.
        /// </summary>
        public Color TileColor { get { return color; } }

        /// <summary>
        /// Return whether or not tile numbers are enabled.
        /// </summary>
        public bool NumbersOn { get { return numbersOn; } }

        /// <summary>
        /// Return whether or not move counter is enabled.
        /// </summary>
        public bool MovesOn { get { return movesOn; } }

        /// <summary>
        /// Return whether or not timer is enabled.
        /// </summary>
        public bool TimerOn { get { return timerOn; } }

        #endregion Fields and Properties

        #region Construction and Reset

        /// <summary>
        /// Construct a CustomGame window to set game parameters.
        /// </summary>
        public CustomGame(SlidingPuzzle mw) {
            InitializeComponent();
            
            mainWindow = mw;              
            resetGameOptions();
            resetCustomizing();
        }

        /// <summary>
        /// Reset game options to match "default game."
        /// </summary>
        private void resetGameOptions() {
            imageFileName = null;
            color = mainWindow.CurrentTileColor;
            tileCount = SlidingPuzzle.DEFAULT_SIZE;
            numbersOn = true;
            timerOn = false;
            movesOn = false;
        }

        /// <summary>
        /// Reset current options window.
        /// </summary>
        private void resetCustomizing() {
            CurrentColorPanel.BackColor = mainWindow.CurrentTileColor;
            NoColorLabel.Hide();
            TileYesRadio.Checked = true;
            TimerNoRadio.Checked = true;
            MovesNoRadio.Checked = true;
            SizeBar.Value = SlidingPuzzle.DEFAULT_SIZE;
            SizeLabel.Text = SizeBar.Value + "";
            DoneButton.DialogResult = DialogResult.OK;
            MyCancelButton.DialogResult = DialogResult.Cancel;
        }

        #endregion Construction and Reset

        #region Options

        /// <summary>
        /// Change tile color.
        /// </summary>
        /// <param name="sender">Color button</param>
        /// <param name="e"></param>
        private void ColorButton_Click(object sender, EventArgs e) {

            // enable and alert color choice:
            NoColorLabel.Hide();
            ColorDialog dlg = new ColorDialog();
            if (dlg.ShowDialog() != DialogResult.Cancel)
                color = dlg.Color;
            CurrentColorPanel.BackColor = color;

            // disable file choice:
            SelectedFileLabel.Text = "not using";
            imageFileName = null;
        }

        /// <summary>
        /// Change/use image file.
        /// </summary>
        /// <param name="sender">Image button</param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, EventArgs e) {
            chooseImageFileName();
            SelectedFileLabel.Text = Path.GetFileName(imageFileName);
            NoColorLabel.Show(); // disable color choice
        }

        /// <summary>
        /// Helper method to show file loading window.
        /// </summary>
        private void chooseImageFileName() {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Use 800x800 for best results."; // not particularly obvious here.

            dlg.Filter =   "bitmap | *.bmp |"      +     // could add even more!
                             "jpeg | .jpg;*.jpeg|" +
                              "GIF | *.gif|"       +
                             "TIFF | *.tif;*.tiff|"+
                        "all files | *.*";
            
            dlg.FilterIndex = 5;                         // show 5 options
            dlg.RestoreDirectory = false;                // let player keep opening same directory

            if (dlg.ShowDialog() == DialogResult.OK) {
                imageFileName = dlg.FileName;
            }

        } // method chooseImageFileName

        /// <summary>
        /// Choose board size (size x size) (2-16, inclusive).
        /// </summary>
        /// <param name="sender">Slider bar</param>
        /// <param name="e"></param>
        private void SizeBar_Scroll(object sender, EventArgs e) {
            tileCount = SizeBar.Value;
            SizeLabel.Text = SizeBar.Value + "";
        }

        #endregion Options

        #region Done

        /// <summary>
        /// If cancel early, need to reenable main window.
        /// </summary>
        /// <param name="sender">Cancel/"X" buttons</param>
        /// <param name="e"></param>
        private void CustomGameWindow_FormClosing(object sender, FormClosingEventArgs e) {
            resetGameOptions();
        }

        /// <summary>
        /// Done entering options. Store user selections before closing window.
        /// </summary>
        /// <param name="sender">Done button</param>
        /// <param name="e"></param>
        private void DoneButton_Click(object sender, EventArgs e) {
            // color or image set by Color/Image box
            // size set by SizeBar
            // so, need to set 3 remaining options:
            numbersOn = TileYesRadio.Checked;
            timerOn = TimerYesRadio.Checked;
            movesOn = MovesYesRadio.Checked;
            Dispose();
        }

        #endregion Done

    } // class CustomGame

} // namespace SlidingPuzzle
